package com.lagou.handler;

import com.lagou.beanutil.SpringContextHolder;
import com.lagou.request.RpcRequest;
import com.lagou.service.UserServiceImpl;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.concurrent.EventExecutorGroup;

import java.lang.reflect.Method;

/**
 * 自定义的业务处理器
 */
public class UserServiceHandler extends ChannelInboundHandlerAdapter {

    //当客户端读取数据时,该方法会被调用
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        RpcRequest rpcRequest=(RpcRequest) msg;
        Object bean = SpringContextHolder.getBean(rpcRequest.getClassName());
        Class<?> aClass = bean.getClass();
        Method method = aClass.getMethod(rpcRequest.getMethodName(), rpcRequest.getParameterTypes());
        Object invoke = method.invoke(bean, rpcRequest.getParameters());
        //3.把调用实现类的方法获得的结果写到客户端
        ctx.writeAndFlush("success");
    }
}
